package Ptolemaiosz;



import java.util.ArrayList;

public class Level {

	private ArrayList<ArrayList<LevelPart>> neighbour2DArray;
	private Application application;
	private LevelPart emptyLevelPart;
	private int numberOfKeys;
        private ArrayList<Avatar> avatars = new ArrayList<Avatar>();

    public Level() {
        	SkeletonLogger.logCall();
                SkeletonLogger.logReturn();
                //Az avatarokat 1-tol szamozzuk, ezert kell egy nulladik elem
                avatars.add(null);
    }

	/**
	 * 
	 * @param levelPart
	 */
         public Application getApplication()
         {
             return application;
         }
    
        public void addAvatar(Avatar a, int id)
            {
                avatars.add(id, a);
            }
        
        public ArrayList<Avatar> getAvatars()
            {
                return avatars;
            }
        
        public void setApplication(Application a)
            {
                application = a;
                SkeletonLogger.logCall();
		SkeletonLogger.logReturn();
            }
        
        public void setEmptyLevelPart(LevelPart l)
            {
                SkeletonLogger.logCall();
		emptyLevelPart = l;
					
		SkeletonLogger.logReturn();                
            }
        
	public ArrayList<ArrayList<LevelPart>> getneighbour2DArray() {
		SkeletonLogger.logCall();
		
		SkeletonLogger.logReturn();
                return neighbour2DArray;
	}
        
        public ArrayList<LevelPart> getLevelParts()
            {
                
                //Sorok szamanak meghatarozasa, ami azert nehezebb, mert eredetileg 10 ueres sor van es a nem uresek szama kell
                int numberOfRows = 0;
                
                for(int i = 0;i < neighbour2DArray.size();i++)
                    {
                        if(neighbour2DArray.get(i).size() != 0)
                            numberOfRows++;
                    }
                
                //Oszlopok szamanak a meghatarozasa
                int numberOfCols = neighbour2DArray.get(0).size();
                
                //Elemek kigyujtese egy listaba
                ArrayList<LevelPart> levelparts = new ArrayList<LevelPart>();

                for(int row = 0; row < numberOfRows;row++)
                    {
                        for(int col = 0; col < numberOfCols;col++)
                            {
                                levelparts.add( neighbour2DArray.get(row).get(col) );
                            }
                    }
                
                return levelparts;
                
            }

	/**
	 * 
	 * @param LevelPart
	 */
	public void setneighbour2DArray(ArrayList<ArrayList<LevelPart>> array) {
		SkeletonLogger.logCall();
                
                neighbour2DArray = array;
		SkeletonLogger.logReturn();
	}

	public LevelPart getEmptyLevelPart() {
		SkeletonLogger.logCall();
		
		SkeletonLogger.logReturn();
                return emptyLevelPart;
	}

	public ArrayList<PhysicalEntity> getPhysicalEntities(LevelPart levelpart) {
		SkeletonLogger.logCall();
		
                //Letrehozunk egy ures tarolot az entitasoknak
                ArrayList<PhysicalEntity> physicalentities = new ArrayList<PhysicalEntity>();
                
                //Lekerjuk az entitasokat az adott palyaszaksztol
                  ArrayList<Element> elements = levelpart.getElements();
	        
                //Minden egyes elementtol lekerjuk a fizikai entitasat, es azt el is taroljuk
                for(int i = 0;i < elements.size();i++)
                    {
                        physicalentities.add(elements.get(i).getPhysicalEntity());
                    }
		
                //Az avatarokat sem felejtjuk el, nekik is van fizikai entitasuk
                
                //Minden avatarra
                for(int i = 1;i <= avatars.size()-1;i++)
                    {
                        //Ha az avatar is ezen a palyaszakasozn van
                        if(avatars.get(i).getCurrentLevelPart().getId() == levelpart.getId())
                            physicalentities.add(avatars.get(i).getPhysicalEntity());
                    }
                            
                 
		SkeletonLogger.logReturn();
 
                //Visszaadjuk az osszegyujtott fizikai entitasokat
                return physicalentities;
	}


	/**
	 * 
	 * @param numberOfKeys
	 */
	public void setNumberOfKeys(int numberOfKeys) {
		SkeletonLogger.logCall();
		
		SkeletonLogger.logReturn();
		
		this.numberOfKeys = numberOfKeys;
	}

	public void keyFound() {
		SkeletonLogger.logCall();
		if(numberOfKeys != 0)
                    numberOfKeys--;
			
		SkeletonLogger.logReturn();
	}

	public void checkLevelCompleted() {
		SkeletonLogger.logCall();

                        //Kimenet
                        if(numberOfKeys == 0)
                            Controller.println("ESEMENY: palya teljesitve");
                        else
                            Controller.println("ESEMENY: nincs minden kulcs felszedve");
                        
			if(numberOfKeys == 0)
                            {
                                application.nextLevel();
                            }
                       
		
		SkeletonLogger.logReturn();
	}

}